import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

class UpFile {
  String guid;
  String spaceId;
  String tag;
  String baseName;
  String dirName;
  String filePath;
  String taskId;
  int done;
  int fType;
  double progress;

  UpFile(this.guid, this.spaceId, this.tag, this.baseName, this.dirName,
      this.filePath, this.taskId, this.done, this.progress, this.fType);

  UpFile.fromJson(Map<String, dynamic> json)
      : guid = json["guid"],
        spaceId = json["spaceId"],
        tag = json["tag"],
        baseName = json["baseName"],
        dirName = json["dirName"],
        filePath = json["filePath"],
        taskId = json["taskId"],
        done = json["done"],
        fType = json["fType"],
        progress = json["progress"];
  Map<String, dynamic> toJson() => {
        'guid': guid,
        'spaceId': spaceId,
        'tag': tag,
        'baseName': baseName,
        'dirName': dirName,
        'filePath': filePath,
        'taskId': taskId,
        'done': done,
        'fType': fType,
        'progress': progress,
      };
}

class UpFileProvider {
  Database? db;
  final String KBMC = 'UpFile';
  final List<String> Columns = [
    'guid',
    'spaceId',
    'tag',
    'baseName',
    'dirName',
    'filePath',
    'taskId',
    'done',
    'fType',
    'progress'
  ];

  Future create() async {
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'UpFile6.db');
    db = await openDatabase(path, version: 1,
        onCreate: (Database db, int version) async {
      await db.execute('''
create table UpFile ( 
  guid text not null,
  spaceId text not null, 
  tag text not null,
 baseName text not null, 
  dirName text not null,
 filePath text not null, 
  taskId text not null,
  done integer not null,
  fType integer not null,
  progress real not null)
''');
    });
  }

  Future<UpFile> insert(UpFile upFile) async {
    await db!.insert(KBMC, upFile.toJson());
    return upFile;
  }

  Future<UpFile?> getOne(String id) async {
    List<Map<String, dynamic>> maps = await db!
        .query(KBMC, columns: Columns, where: 'guid= ?', whereArgs: [id]);
    if (maps.length > 0) {
      return UpFile.fromJson(maps.first);
    }
    return null;
  }

  Future<UpFile?> getByTaskId(String taskId) async {
    List<Map<String, dynamic>> maps = await db!
        .query(KBMC, columns: Columns, where: 'taskId= ?', whereArgs: [taskId]);
    if (maps.length > 0) {
      return UpFile.fromJson(maps.first);
    }
    return null;
  }

  Future<List<UpFile>?> getList() async {
    List<Map<String, dynamic>> maps = await db!.query(KBMC, columns: Columns);

    var list1 = <UpFile>[];

    for (var x in maps) {
      list1.add(UpFile.fromJson(x));
    }

    return list1;
  }

  Future<int> delete(String id) async {
    return await db!.delete(KBMC, where: 'guid = ?', whereArgs: [id]);
  }

  Future<int> update(UpFile upFile) async {
    return await db!.update(KBMC, upFile.toJson(),
        where: 'guid = ?', whereArgs: [upFile.guid]);
  }

  Future close() async => db!.close();
}
